package com.dd.cloud.user.service.log.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.IPUtil;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.log.LogException;
import com.dd.cloud.user.entity.log.LogModul;
import com.dd.cloud.user.mapper.log.LogExceptionMapper;
import com.dd.cloud.user.mq.MQConfig;
import com.dd.cloud.user.mq.RabbitSend;
import com.dd.cloud.user.req.log.LogExceptionPageReq;
import com.dd.cloud.user.res.log.LogExceptionPageRes;
import com.dd.cloud.user.service.log.ILogExceptionService;
import com.dd.cloud.user.service.log.ILogModulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统异常日志表,系统抛出的异常 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
public class LogExceptionServiceImpl extends ServiceImpl<LogExceptionMapper, LogException> implements ILogExceptionService {
    @Autowired
    private RabbitSend rabbitSend;
    @Autowired
    private ILogModulService logModulService;

    /**
     * 将异常消息添加进消息队列
     *
     * @param logExc
     */
    @Override
    public void addLogExcMQ(LogException logExc) {
        rabbitSend.sendMessage(MQConfig.LOG_EXC_EXCHANGE, MQConfig.LOG_EXC_QUERE, logExc);
    }

    @Override
    public boolean addLogExc(LogException logExc) {
        if (logExc == null) {
            return false;
        }
        if (logExc.getExcMsg() != null && logExc.getExcMsg().length() > 2048) {
            logExc.setExcMsg(logExc.getExcMsg().substring(0, 2048));
        }
        if (logExc.getExcName() != null && logExc.getExcName().length() > 255) {
            logExc.setExcName(logExc.getExcName().substring(0, 255));
        }
        if (logExc.getOperUrl() != null && logExc.getOperUrl().length() > 100) {
            logExc.setOperUrl(logExc.getOperUrl().substring(0, 100));
        }
        if (logExc.getOperMethod() != null && logExc.getOperMethod().length() > 128) {
            logExc.setOperMethod(logExc.getOperMethod().substring(0, 128));
        }
        if (logExc.getOperReqParams() != null && logExc.getOperReqParams().length() > 512) {
            logExc.setOperReqParams(logExc.getOperReqParams().substring(0, 512));
        }
        if (logExc.getOperDesc() != null && logExc.getOperDesc().length() > 200) {
            logExc.setOperDesc(logExc.getOperDesc().substring(0, 200));
        }
        if (logExc.getOperUrl() != null) {
            LogModul logModul = logModulService.getLogModulByUrl(logExc.getOperUrl());
            logExc.setModular(logModul.getModular());
            logExc.setOperType(logModul.getType());
            logExc.setOperDesc(logModul.getOperDesc());
            logExc.setRemarks(logModul.getRemarks());
        }
        IPUtil.IpAddr ipAddr = IPUtil.getIpAddr(logExc.getOperIp());
        if (ipAddr != null) {
            logExc.setOperIpAddr(ipAddr.getDetail());
        }
        try {
            return save(logExc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询异常日志记录
     *
     * @param req
     * @return
     */
    @Override
    public IPage<LogExceptionPageRes> queryLogExceptionPage(LogExceptionPageReq req) {
        if (req.verify()) {
            throw new ZekeException(400, "日期参数有误");
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        IPage<LogExceptionPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryLogExceptionPage(iPage, req);
        //智尔康管理员, 日志参数字段有手机号,身份证号等敏感信息, 去掉此字段
        if (loginInfo.getType() == LoginInfo.UserType.ADMIN && loginInfo.getId() == 33){
            List<LogExceptionPageRes> collect = iPage.getRecords().stream().map(item -> {
                item.setOperReqParams(null);
                return item;
            }).collect(Collectors.toList());
            iPage.setRecords(collect);
        }
        return iPage;
    }
}
